package zld369504.tw.m_team;

import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.squareup.picasso.Transformation;

import java.util.ArrayList;
import java.util.List;

import zld369504.tw.m_team.dao.Torrent;

public class TorrentListRecyclerViewAdapter extends RecyclerView.Adapter<TorrentListRecyclerViewAdapter.TorrentViewHolder> {

    private OnRecyclerViewItemClickListener itemClickedListener;
    private Context mContext;

    private List<Torrent> list;

    public TorrentListRecyclerViewAdapter(Context context) {
        this.mContext = context;
        list = new ArrayList<>();
    }

    public void setOnItemClickListener(OnRecyclerViewItemClickListener itemClickedListener) {
        this.itemClickedListener = itemClickedListener;
    }

    public void setList(List<Torrent> list) {
        this.list.clear();
        this.list.addAll(list);
    }

    public void addList(List<Torrent> list) {
        this.list.addAll(list);
    }

    private static Transformation getTransformation(final ImageView view) {
        return new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                int targetWidth = view.getWidth();

                //返回原图
                if (source.getWidth() == 0 || source.getWidth() < targetWidth) {
                    return source;
                }

                //如果图片大小大于等于设置的宽度，则按照设置的宽度比例来缩放
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (targetWidth * aspectRatio);
                if (targetHeight == 0 || targetWidth == 0) {
                    return source;
                }
                Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
                if (result != source) {
                    // Same bitmap is returned if sizes are the same
                    source.recycle();
                }
                return result;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        };
    }

    private class TorrentThumbClickListener implements View.OnClickListener {

        private int position;

        TorrentThumbClickListener(int position) {
            this.position = position;
        }

        @Override
        public void onClick(View view) {
            Torrent torrent = list.get(position);
            Intent intent = new Intent(mContext, ShowImageActivity.class);
            intent.putExtra("URL", torrent.getImageUrl());
            mContext.startActivity(intent);
        }
    }

    public Torrent getItem(int position){
        return list.get(position);
    }

    @Override
    public TorrentViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_torrent_list, parent, false);
        return new TorrentViewHolder(rootView);
    }

    @Override
    public void onBindViewHolder(TorrentViewHolder holder, int position) {
        Torrent torrent = list.get(position);

        Picasso.with(mContext)
                .load(torrent.getImageUrl())
                .transform(getTransformation(holder.ivTorrentPic))
                .into(holder.ivTorrentPic);

        if (torrent.isNewTorrent()) {
            holder.ivNew.setVisibility(View.VISIBLE);
            holder.ivHot.setVisibility(View.GONE);
        } else {
            holder.ivNew.setVisibility(View.GONE);
            if (torrent.isHot()) {
                holder.ivHot.setVisibility(View.VISIBLE);
            } else {
                holder.ivHot.setVisibility(View.GONE);
            }
        }

        holder.ivTorrentPic.setOnClickListener(new TorrentThumbClickListener(position));

        switch (torrent.getStickyTopLevel()){
            case 0:
                holder.llTorrentBackground.setBackgroundColor(mContext.getResources().getColor(R.color.m_material_grey_50));
                break;
            case 1:
                holder.llTorrentBackground.setBackgroundColor(mContext.getResources().getColor(R.color.colorStickyNormalTorrentListItemBackground));
                break;
            case 2:
                holder.llTorrentBackground.setBackgroundColor(mContext.getResources().getColor(R.color.colorStickyTopTorrentListItemBackground));
                break;
        }
        holder.tvTorrentType.setText(torrent.getTypeName());

        holder.tvTorrentTitle.setText(torrent.getTitle());
        holder.tvTorrentSubtitle.setText(torrent.getSubTitle());
        holder.tvSize.setText(torrent.getSize());
        switch (torrent.getDiscount()) {
            case 100:
                holder.llDiscount.setVisibility(View.VISIBLE);
                holder.ivDiscount.setImageResource(R.drawable.pro_free);
                break;
            case 50:
                holder.llDiscount.setVisibility(View.VISIBLE);
                holder.ivDiscount.setImageResource(R.drawable.pro_50pctdown);
                break;
            case 30:
                holder.llDiscount.setVisibility(View.VISIBLE);
                holder.ivDiscount.setImageResource(R.drawable.pro_30pctdown);
                break;
            default:
                holder.llDiscount.setVisibility(View.GONE);
                break;
        }
        if (torrent.isIncludeChineseSubtitle()) {
            holder.llChineseSub.setVisibility(View.VISIBLE);
        } else {
            holder.llChineseSub.setVisibility(View.GONE);
        }

        if (torrent.isIncludeChineseTrack()) {
            holder.llChineseTrack.setVisibility(View.VISIBLE);
        } else {
            holder.llChineseTrack.setVisibility(View.GONE);
        }

        if (torrent.isDiy()) {
            holder.llDIY.setVisibility(View.VISIBLE);
        } else {
            holder.llDIY.setVisibility(View.GONE);
        }

        if (torrent.isOfficial()) {
            holder.ivOfficial.setVisibility(View.VISIBLE);
        } else {
            holder.ivOfficial.setVisibility(View.GONE);
        }

        int uploadingCount = torrent.getUploadingCount();
        holder.tvUploadingCount.setText(String.valueOf(uploadingCount));
        if (uploadingCount <= 3) {
            holder.tvUploadingCount.setTextColor(Color.parseColor("#ff0000"));
            holder.tvUploadingCount.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
        } else {
            holder.tvUploadingCount.setTextColor(Color.parseColor("#333333"));
            holder.tvUploadingCount.setTypeface(Typeface.defaultFromStyle(Typeface.NORMAL));
        }

        holder.tvDownloadingCount.setText(String.valueOf(torrent.getDownloadingCount()));
        holder.tvFinishCount.setText(String.valueOf(torrent.getFinishedCount()));
        holder.tvTtlTime.setText(torrent.getTtlTime());

        if (!TextUtils.isEmpty(torrent.getDiscountTimeLeft())) {
            holder.tvDiscountTimeLeft.setVisibility(View.VISIBLE);
            holder.tvDiscountTimeLeft.setText(String.format("%c%s%c",'(',torrent.getDiscountTimeLeft(),')'));
        } else {
            holder.tvDiscountTimeLeft.setVisibility(View.GONE);
        }
    }

    @Override
    public int getItemCount() {
        return list == null ? 0 : list.size();
    }

    class TorrentViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

        ImageView ivTorrentPic;
        TextView tvTorrentTitle;
        TextView tvTorrentSubtitle;
        TextView tvSize;
        ImageView ivDiscount;
        TextView tvUploadingCount;
        TextView tvDownloadingCount;
        TextView tvFinishCount;
        TextView tvTtlTime;
        TextView tvDiscountTimeLeft;
        TextView tvTorrentType;
        ImageView ivHot;
        ImageView ivNew;
        ImageView ivOfficial;

        LinearLayout llDiscount;
        LinearLayout llChineseSub;
        LinearLayout llChineseTrack;
        LinearLayout llDIY;
        LinearLayout llTorrentBackground;

        public TorrentViewHolder(View rootView) {
            super(rootView);
            ivTorrentPic = (ImageView) rootView.findViewById(R.id.ivTorrentPic);
            tvTorrentTitle = (TextView) rootView.findViewById(R.id.tvTorrentTitle);
            tvSize = (TextView) rootView.findViewById(R.id.tvSize);
            ivDiscount = (ImageView) rootView.findViewById(R.id.ivDiscount);
            tvUploadingCount = (TextView) rootView.findViewById(R.id.tvUploadingCount);
            tvDownloadingCount = (TextView) rootView.findViewById(R.id.tvDownloadingCount);
            tvFinishCount = (TextView) rootView.findViewById(R.id.tvFinishCount);
            tvTtlTime = (TextView) rootView.findViewById(R.id.tvTtlTime);
            tvTorrentSubtitle = (TextView) rootView.findViewById(R.id.tvTorrentSubtitle);
            tvDiscountTimeLeft = (TextView) rootView.findViewById(R.id.tvDiscountTimeLeft);
            tvTorrentType = (TextView) rootView.findViewById(R.id.tvTorrentType);
            ivHot = (ImageView) rootView.findViewById(R.id.ivHot);
            ivNew = (ImageView) rootView.findViewById(R.id.ivNew);
            ivOfficial = (ImageView) rootView.findViewById(R.id.ivOfficial);

            llDiscount = rootView.findViewById(R.id.llDiscount);
            llChineseSub = rootView.findViewById(R.id.llChineseSub);
            llChineseTrack = rootView.findViewById(R.id.llChineseTrack);
            llDIY = rootView.findViewById(R.id.llDIY);
            llTorrentBackground = rootView.findViewById(R.id.llTorrentBackground);

            rootView.setOnClickListener(this);
        }

        @Override
        public void onClick(View view) {
            if (itemClickedListener != null) {
                itemClickedListener.onItemClick(view, getPosition());
            }
        }
    }
}
